package cn.attackme.myuploader.controller;


import cn.attackme.myuploader.model.User;
import cn.attackme.myuploader.service.UserService;
import cn.attackme.myuploader.utils.ResultUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/find")
    public String find(String username) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        User user=userService.getUser(username);
        Map<String,Object> resultMap = ResultUtils.getResponse(user,"succeed",
                "internal error",false);
        String str  = mapper.writeValueAsString(resultMap);
        System.out.println(str);
        return str;
    }

    @PostMapping("/add")
    public Map<String,Object> add(User user) throws IOException {

        int  flag =userService.save(user);

        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/update")
    public  Map<String,Object> update(User user) throws IOException {
        System.out.println(user);
        int  flag =userService.update(user);
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/delete")
    public  Map<String,Object>  delete(String username) throws IOException {
        int  flag =userService.delete(username);
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/login")
    public  Map<String,Object> login(User user) throws IOException {
        User testUser=userService.login(user.getUsername(),user.getPassword());
        if(testUser ==  null ){
            Map<String,Object> resultMap = ResultUtils.getResponse("on","succeed",
                    "internal error",false);
            return resultMap;
        }else{
            Map<String,Object> resultMap = ResultUtils.getResponse("ok","succeed",
                    "internal error",false);
            return resultMap;
        }
    }

    @PostMapping("/all")
    public  Map<String,Object> all() throws IOException {
        List<Map<String,Object>> userList = userService.findAll();
        Map<String,Object> resultMap = ResultUtils.getResponse(userList,"succeed",
                "internal error",false);
        return resultMap;

    }

}
